package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;

    /*
     * 新增套餐及对应的菜品
     */
    @Transactional
    public void saveWithDish(SetmealDTO setmealDTO) {
        //数据拷贝
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //向套餐表插入数据
        setmealMapper.insert(setmeal);

        //新增套餐后获取套餐的id
        Long setmealId = setmeal.getId();

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes ){
                setmealDish.setSetmealId(setmealId);
        }
        //向套餐菜品关系表插入多条数据
        setmealDishMapper.insertBatch(setmealDishes);
    }

    /*
     * 根据id查询套餐
     */
    @Transactional
    public SetmealVO getById(Long id) {
        //根据id查询对应的套餐数据
        Setmeal setmeal = setmealMapper.getById(id);

        SetmealVO setmealVO = new SetmealVO();

        //数据拷贝
        BeanUtils.copyProperties(setmeal,setmealVO);

        //根据要查询套餐的分类id到category(分类表)中查询分类信息，从而获取对应分类名称
        Category category = categoryMapper.getById(setmealVO.getCategoryId());

        //封装获取的分类名称
        setmealVO.setCategoryName(category.getName());

        //根据套餐id查询套餐菜品表中的信息，返回一个List对象
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(setmealVO.getId());

        //封装与套餐关联的菜品信息
        setmealVO.setSetmealDishes(setmealDishes);

        return setmealVO;
    }

    /*
     * 套餐分页查询
     */
    public PageResult pageQueryDto(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /*
     * 修改套餐
     */
    @Transactional
    public void updateWithDish(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        //修改套餐表中的基本数据
        setmealMapper.update(setmeal);

        //删除原套餐表对应的菜品数据
        setmealDishMapper.deleteBySetmealId(setmeal.getId());

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes){
                setmealDish.setSetmealId(setmeal.getId());
        }
        setmealDishMapper.insertBatch(setmealDishes);
    }

    /*
     * 套餐起售、停售
     */
    public void startOrStop(Integer status, Long id) {

        //起售操作，如果套餐中的菜品处于停售状态，无法起售
        if(status == StatusConstant.ENABLE){
            List<Dish> dishes = dishMapper.getBySetmealId(id);
            for (Dish dish : dishes){
                if (dish.getStatus() == StatusConstant.DISABLE){
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            }
        }

        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
        setmealMapper.update(setmeal);
    }

    /*
     * 批量删除套餐
     */
    @Transactional
    public void deleteBatch(List<Long> ids) {
        //起售状态的套餐不能删除
        for (Long id : ids){
            Setmeal setmeal = setmealMapper.getById(id);
            if(setmeal.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        //根据套餐id批量删除套餐表的数据
        setmealMapper.deleteBatch(ids);
        //根据套餐id批量删除关联的菜品信息
        setmealDishMapper.deleteBatch(ids);
    }

    /*
     * 根据分类id查询起售的套餐
     */
    public List<Setmeal> getByCategoryId(Setmeal setmeal) {
        List<Setmeal> setmeals = setmealMapper.getBySetmeal(setmeal);
        return setmeals;
    }

    /*
     * 根据套餐id查询套餐包含的菜品
     */
    public List<DishItemVO> getByIdWithDish(Long setmealId) {
        //根据套餐的id查询相关的菜品
/*        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(setmealId);

        List<DishItemVO>  dishItemVOS = new ArrayList<>();

        for(SetmealDish setmealDish:setmealDishes){
            DishItemVO dishItemVO = new DishItemVO();
            BeanUtils.copyProperties(setmealDish,dishItemVO);
            //根据菜品的名称查询菜品的图片路径和描述信息       mapper接口中的方法已删除
            Dish dish = dishMapper.getByName(dishItemVO.getName());
            dishItemVO.setImage(dish.getImage());
            dishItemVO.setDescription(dish.getDescription());

            dishItemVOS.add(dishItemVO);
        }*/

//        return dishItemVOS;


         // 根据套餐id和菜品分类id查询dish表和setmealDish表中所需字段
         return  dishMapper.getListByStemealId(setmealId);

    }


}
